package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.PublishInfoDao;
import com.lucky.ebidding.dao.TbInfoDao;
import com.lucky.ebidding.dao.ZbInfoDao;
import com.lucky.ebidding.dao.ZbModifyDao;
import com.lucky.ebidding.dto.ZbInfoAndModify;
import com.lucky.ebidding.enums.NotificationTypeEnum;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.interfaces.ZbServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * ClassName:ZbService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/3/18 14:18
 * @Author:857251389@qq.com
 */
@Service
public class ZbService implements ZbServiceI {

    @Autowired
    private ZbInfoDao zbInfoDao;

    @Autowired
    private PublishInfoDao publishInfoDao;

    @Autowired
    private TbService tbService;

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private ZbModifyDao zbModifyDao;
    @Override
    public void insert(ZbInfo zbInfo) {
        //如果招标项目没有id,证明是第一次添加
        if (zbInfo.getId() == null) {
            zbInfo.setProjectId(UUID.randomUUID().toString().substring(0, 8)+LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            int i = zbInfoDao.insertSelective(zbInfo);
        }else {//否则是修改信息操作
            int i = zbInfoDao.updateByPrimaryKeySelective(zbInfo);
            List<TbInfo> tbInfos = tbService.selectTbInfoById(zbInfo.getId());
            if(tbInfos.size()!=0){
                for (TbInfo tbInfo : tbInfos) {
                    notificationService.sendNotification(tbInfo.getUserId(),zbInfo.getUserId(), NotificationTypeEnum.ZBINFO_UPDATE.getType(),zbInfo.getId(),"公告信息");
                }
            }
        }
    }

    @Override
    public List<ZbInfo> selectAll() {

        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(new ZbInfoExample());

        return zbInfos;
    }

    @Override
    public List<ZbInfo> selectByUserId(Integer id) {

        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andUserIdEqualTo(id);

        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);

        return zbInfos;
    }

    @Override
    public ZbInfo selectById(Integer id) {
        ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(id);
        return zbInfo;
    }

    @Override
    public List<ZbInfo> selectZbInfoByUserId(Integer id) {
        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andUserIdEqualTo(id);
        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);
        return zbInfos;
    }

    @Override
    public int InsertPublishInfo(Integer zbId, Integer tbId) {
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setZbId(zbId);
        publishInfo.setTbId(tbId);

        PublishInfoExample publishInfoExample = new PublishInfoExample();
        publishInfoExample.createCriteria().andZbIdEqualTo(zbId).andTbIdEqualTo(tbId);
        List<PublishInfo> publishInfos = publishInfoDao.selectByExample(publishInfoExample);
        if(publishInfos.isEmpty()){
            int i = publishInfoDao.insertSelective(publishInfo);
            return i;
        }else {
            return 0;
        }
    }

    @Override
    public List<ZbInfo> selectByKbFlag(int i) {

        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andKbFlagEqualTo(i);
        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);
        return zbInfos;
    }

    @Override
    public int setStatus(Integer status, Integer zbId) {
        ZbInfo zbInfo = new ZbInfo();
        zbInfo.setId(zbId);
        zbInfo.setShFlag(status);
        int i = zbInfoDao.updateByPrimaryKeySelective(zbInfo);
        return i;
    }

    @Override
    public List<ZbInfo> selectByShFlag(int i) {
        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andShFlagEqualTo(i);
        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);
        return zbInfos;
    }

    @Override
    public int uppdateKbFlag(Integer zbId) {
        ZbInfo zbInfo = new ZbInfo();
        zbInfo.setId(zbId);
        zbInfo.setKbFlag(1);
        int i = zbInfoDao.updateByPrimaryKeySelective(zbInfo);
        return i;
    }

    @Override
    public List<ZbInfo> selectByShFlagAndKbFlag(int i, int i1) {
        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andShFlagEqualTo(i).andKbFlagEqualTo(i1);
        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);
        return zbInfos;
    }

    @Override
    public void insertZbModify(ZbModify zbModify) {

        zbModifyDao.insertSelective(zbModify);
    }

    @Override
    public List<ZbInfoAndModify> selectZbInfoAndZbModify() {
        List<ZbModify> zbModifies = zbModifyDao.selectByExample(new ZbModifyExample());
        List<ZbInfoAndModify> zbInfoAndModifies = new ArrayList<>();
        for (ZbModify zbModify : zbModifies) {
            ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(zbModify.getZbId());
            ZbInfoAndModify zbInfoAndModify = new ZbInfoAndModify();
            zbInfoAndModify.setZbInfo(zbInfo);
            zbInfoAndModify.setZbModify(zbModify);
            zbInfoAndModifies.add(zbInfoAndModify);
        }
        return zbInfoAndModifies;
    }

    @Override
    public List<ZbInfoAndModify> selectZbInfoAndZbModifyByShFlag(Integer shFlag) {
        ZbModifyExample zbModifyExample = new ZbModifyExample();
        zbModifyExample.createCriteria().andShFlagEqualTo(1);

        List<ZbModify> zbModifies = zbModifyDao.selectByExample(zbModifyExample);

        List<ZbInfoAndModify> zbInfoAndModifies = new ArrayList<>();
        for (ZbModify zbModify : zbModifies) {
            ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(zbModify.getZbId());
            ZbInfoAndModify zbInfoAndModify = new ZbInfoAndModify();
            zbInfoAndModify.setZbInfo(zbInfo);
            zbInfoAndModify.setZbModify(zbModify);
            zbInfoAndModifies.add(zbInfoAndModify);
        }
        return zbInfoAndModifies;
    }

    @Override
    public List<ZbInfoAndModify> selectZbInfoAndZbModifyByzbId(Integer zbId) {
        ZbModifyExample zbModifyExample = new ZbModifyExample();
        zbModifyExample.createCriteria().andZbIdEqualTo(zbId);
        List<ZbModify> zbModifies = zbModifyDao.selectByExample(zbModifyExample);
        List<ZbInfoAndModify> zbInfoAndModifies = new ArrayList<>();
        for (ZbModify zbModify : zbModifies) {
            ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(zbModify.getZbId());
            ZbInfoAndModify zbInfoAndModify = new ZbInfoAndModify();
            zbInfoAndModify.setZbInfo(zbInfo);
            zbInfoAndModify.setZbModify(zbModify);
            zbInfoAndModifies.add(zbInfoAndModify);
        }
        return zbInfoAndModifies;
    }

    @Override
    public int setZbModifyStatus(Integer status, Integer zbUsedId,Integer zbId, Integer modifyId, User user) {
        ZbModify zbModify = new ZbModify();
        zbModify.setId(modifyId);
        zbModify.setShFlag(status);
        int i = zbModifyDao.updateByPrimaryKeySelective(zbModify);
        String content;
        if(i!=0){
            if(status == 1){
                content = "通过";
            }else{
                content = "不通过";
            }
            notificationService.sendNotification(zbUsedId,user.getId(),NotificationTypeEnum.ZBINFO_UPDATE_CHECK.getType(),zbId,content);
        }
        return i;
    }

    @Override
    public int isSh(Integer zbId) {

        ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(zbId);
        if(zbInfo.getShFlag()==0||zbInfo.getShFlag()==2){
            return 0;
        }
        return 1;
    }

}
